﻿using Cysharp.Threading.Tasks;

namespace Runtime.Net {

    public static class DFR_NetWebRequest {

        public static async UniTask<string> Get(string url) {
#if UNITY_EDITOR
            UnityEngine.Debug.Log($"[{nameof(DFR_NetWebRequest)}({nameof(url)}={url})]");
#endif
            var webRequest = UnityEngine.Networking.UnityWebRequest.Get(url);
            try {
                await webRequest.SendWebRequest();
            } catch (UnityWebRequestException e) {
                UnityEngine.Debug.LogException(e);
                return null;
            }
            if (webRequest.isDone && webRequest.result == UnityEngine.Networking.UnityWebRequest.Result.Success) {
                return webRequest.downloadHandler.text;
            }
            return null;
        }
    }
}